package diploma.kisel.modelas.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import diploma.kisel.hibernate.util.HibernateUtil;
import diploma.kisel.modelas.domain.User;
import diploma.kisel.modelas.domain.Workplace;
import diploma.kisel.modelas.repository.api.WorkplaceRepository;

public class WorkplaceRepositoryImpl implements WorkplaceRepository {
	
	EntityManager em = HibernateUtil.getEm();

	@Override
	public List<Workplace> findAll() {
		List<Workplace> workplaces = em.createNamedQuery("Workplace.findAll",
				Workplace.class).getResultList();
		return workplaces;
	}

	@Override
	public Workplace findById(int id) {
		TypedQuery<Workplace> query= em.createNamedQuery("Workplace.findById", Workplace.class);
		query.setParameter("id", id);
		return query.getSingleResult();
	}

	@Override
	public Workplace save(Workplace workplace) {
		em.getTransaction().begin();
		em.persist(workplace);
		em.getTransaction().commit();
		return workplace;
	}

	@Override
	public Workplace update(Workplace workplace) {
		em.getTransaction().begin();
		em.merge(workplace);
		em.getTransaction().commit();
		return workplace;
	}

	@Override
	public void remove(Workplace workplace) {
		em.getTransaction().begin();
		em.remove(workplace);
		em.getTransaction().commit();				
	}

}
